package com.aube.mis.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.api.untrans.aubepro.feedback.FeedbackVoService;
import com.aube.api.untrans.aubepro.feedback.resp.FeedbackRespVo;
import com.aube.common.resp.vo.PaginationRespVo;
import com.aube.mis.dto.FeedbackDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.FeedbackService;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;

@Service
@SuppressWarnings("unchecked")
public class FeedbackServiceImpl extends AbstractService implements FeedbackService {

	private static Logger log = LoggerFactory.getLogger(FeedbackServiceImpl.class);

	@Resource
	FeedbackVoService feedbackVoService;
	
	@Override
	public PaginationResultDTO<FeedbackDTO> getFeedbackList(DataTableCriterias criterias, String showId, String replyStatus) {
		ResultCode<PaginationRespVo<FeedbackRespVo>> serverResp = feedbackVoService.queryListByReplyStatus(showId, replyStatus, criterias.getStartPage(), criterias.getLength());
		List<FeedbackDTO> resultList = (List<FeedbackDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public FeedbackDTO transform(Object object) {
				FeedbackDTO result = convertFeedbackDTO((FeedbackRespVo) object);
				return result;
			}
		});
		PaginationResultDTO<FeedbackDTO> result = new PaginationResultDTO<FeedbackDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	private FeedbackDTO convertFeedbackDTO(FeedbackRespVo vo) {
		FeedbackDTO result = new FeedbackDTO();
		try {
			BeanUtils.copyProperties(result, vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.setShowId(vo.getShowid());
		return result;
	}

	@Override
	public FeedbackDTO getFeedbackDetail(String showId, String feedbackId) {
		ResultCode<FeedbackRespVo> result = feedbackVoService.getDetailById(showId, feedbackId);
		return convertFeedbackDTO(result.getRetval());
	}
	
	@Override
	public void reply(String feedbackId, String content) {
		feedbackVoService.replyFeedback(feedbackId, content);
	}
	
}
